<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2021/7/27
 * Time: 10:46
 */

namespace app\index\controller;

use app\common\controller\Frontend;
use app\admin\model\article\Article;
use app\admin\model\article\Comment;
use app\admin\model\article\Buy;
use app\common\model\User;
use think\Db;

class Tag extends Frontend
{
    protected $noNeedLogin = ['index', 'pinglun', 'detail'];
    protected $noNeedRight = ['*'];


    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\article\Comment;

    }

    public function index()
    {
        $title = input('param.title');
        $category_id = input('param.category_id');

        $Article = new Article;
        $list = $Article
            ->with(['fenlei'])
            ->where(['is_shopdata'=>1,'fenlei_id'=>$category_id,'status'=>1])
            ->order('id desc')
            ->paginate(15);

        foreach ($list as $row) {
            $row->visible(['id','title','path_imgimage','price','zhu_images','pageviews','price']);
            $row->visible(['fenlei']);
            $row->getRelation('fenlei')->visible(['title']);
        }

        // 获取分页显示
        $page = $list->render();
        // 模板变量赋值
        $total = $list?$list->toArray()['total']:0;

        $this->assign('total',$total);

        $this->assign('list', $list);

        $this->assign('page', $page);

        $this->assign('title',$title);
        return $this->view->fetch();
    }

    /*
     * 已购买列表
     */
    public function buyList()
    {
        $list = db::name('buy')->alias('a')
                ->join('img_article b ','a.article_id=b.id')
                ->field('b.*')
                ->where('user_id',$this->auth->id)
                ->paginate(8);

        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->view->fetch();
    }

    public function comment()
    {
        $content = input('param.content');
        $article_id = input('param.article_id');
        $user_id = input('param.user_id');

        $comment = new Comment([
            'article_id'=>$article_id,
            'content'=>$content,
            'user_id'=>$user_id
        ]);
        $res = $comment->save();

        if($res)
        {
            return json(['code'=>1,'date'=>date('Y-m-d H:i',time())]);
        }
        return json(['code'=>0]);

    }

    public function pinglun()
    {
        $article_id = input('param.article_id');

        $list = $this->model
            ->with(['article','user'])
            ->where('comment.article_id',$article_id)
            ->order('comment.id','desc')
            ->select();

        foreach ($list as $row) {
            $row->visible(['id','content','createtime']);
            $row->visible(['article']);
            $row->getRelation('article')->visible(['title']);
            $row->visible(['user']);
            $row->getRelation('user')->visible(['nickname']);
        }
       return json(['list'=>$list]);
    }

    public function buy()
    {
        $article_id = input('param.article_id');
        $userInfo = $this->auth->getUserinfo();
        $info = Article::get($article_id);
        if($info['is_shopdata']==2)
        {
            return json(['code'=>0,'msg'=>'已被购买']);
        }

        if($info['user_id']==$this->auth->id)
        {
            return json(['code'=>0,'msg'=>'不能购买自己作品']);
        }

        if(floatval($userInfo['money'])<floatval($info['price']))
        {
            return json(['code'=>0,'msg'=>'余额不足']);
        }
        Db::startTrans();
        try {
            $buy = new Buy([
                'article_id'  =>  $article_id,
                'user_id' =>  $this->auth->id,
            ]);
            $res = $buy->save();
            if($res)
            {
                $info->is_shopdata = 2;
                $info->save();
//                $user = new User;

                User::reduce_money(floatval($info['price']),$this->auth->id,'购买图画');
                if($info['is_auth']==2)
                {
                    User::money(floatval($info['price']),$info['user_id'],'购买图画');
                }

            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            return json(['code'=>0,'msg'=>'购买失败']);
            Db::rollback();
        }
        return json(['code'=>1,'msg'=>'购买成功']);

    }

    public function detail()
    {
        $type = input('param.type',1);
        $id = input('param.id');
        $info = Article::get($id);
        $link = $info->pageviews;
        $info->pageviews	=  Db::raw('pageviews+1');
        $info->save();
        $info->link =$link;

        $fenlei_title = Db::name('fenlei')->where('id',$info['fenlei_id'])->value('title');
        $this->assign('fenlei_title',$fenlei_title);
        $this->assign('info',$info);
        $this->assign('type',$type);
        return $this->view->fetch();
    }

    public function article()
    {

    }

}